package com.kma.ncpractice2013.servlets;

import com.kma.ncpractice2013.dao.PromoCodeDAO;
import com.kma.ncpractice2013.dao.PromoCodeTypeDAO;
import com.kma.ncpractice2013.dao.UserDAO;
import com.kma.ncpractice2013.dao.UserGroupDAO;
import com.kma.ncpractice2013.model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/*
 * User: Illyas
 * Date: 11/13/13
 * Time: 10:07 PM
 */

public class ManagerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        if (action.equals("saveDiscounts"))
        {
            String nonVIPDiscount = request.getParameter("nonVIPDiscount"),
                VIPDiscount = request.getParameter("VIPDiscount");
                           out.print(VIPDiscount);
            UserGroupDAO dao = new UserGroupDAO();
            UserGroup group = dao.getById(User.Group.ordinaryCustomer.getValue());
            group.setDiscount(Integer.parseInt(nonVIPDiscount));
            dao.update(group);
            group = dao.getById(User.Group.VIPCustomer.getValue());
            group.setDiscount(Integer.parseInt(VIPDiscount));
            dao.update(group);
        }
        else if (action.equals("saveCommission"))
        {
            Integer commission = Integer.parseInt(request.getParameter("commission"));
            Commission.setValue(commission);
        }
        else if (action.equals("generatePromocodes"))
        {
            PrintWriter w = new PrintWriter("D:\\file.txt");
            PromoCodeType type = new PromoCodeType();
            Integer discount = Integer.parseInt(request.getParameter("discount"));
            w.println(discount);
            type.setDiscount(discount);
            String ssDate = request.getParameter("startDate"),
                seDate = request.getParameter("endDate");
            w.println(ssDate);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd");
            try {
                Date sDate = format.parse(ssDate);
                type.setStartDate(sDate);
                Date eDate = format.parse(seDate);
                type.setExpirationDate(eDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            HttpSession session = request.getSession();
            String login = session.getAttribute("username").toString();
            w.println(login);
            UserDAO userDao = new UserDAO();
            User manager = userDao.getByLogin(login);
            int id = manager.getId();
            type.setManagerId(id);
            PromoCodeTypeDAO typeDao = new PromoCodeTypeDAO();
            typeDao.create(type);
            Integer typeId = typeDao.getLastId();
            w.println(typeId);
            Integer number = Integer.parseInt(request.getParameter("number"));
            PromoCodeDAO codeDao = new PromoCodeDAO();
            for (int i = 0; i < number; ++i)
            {
                PromoCode code = new PromoCode();
                code.setTypeId(typeId);
                code.setCode(UUID.randomUUID().toString().substring(0,20));
                w.println(code);
                codeDao.create(code);
            }


        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
